package io.renren.sf.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.renren.sf.entity.CbAppreciationPictureEntity;
import io.renren.sf.entity.CbChapterPictureEntity;
import io.renren.sf.service.CbAppreciationPictureService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.entity.CbAppreciationEntity;
import io.renren.sf.service.CbAppreciationService;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-03-31 13:04:12
 */
@RestController
@RequestMapping("sf/cbappreciation")
public class CbAppreciationController {
    @Autowired
    private CbAppreciationService cbAppreciationService;
    @Autowired
    private CbAppreciationPictureService cbAppreciationPictureService;

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbappreciation:info")
    public R info(@PathVariable("id") Long id){
		CbAppreciationEntity cbAppreciation = cbAppreciationService.getById(id);
        return R.ok().put("cbAppreciation", cbAppreciation);
    }

    /**
     * 信息
     */
    @GetMapping("/getAppreciationByWordId/{id}")
    //@RequiresPermissions("sf:cbappreciation:info")
    public R getAppreciationByWordId(@PathVariable("id") Long id){
        List<CbAppreciationEntity> xsList = cbAppreciationService.getAppreciationByWordId(id);
        return R.ok().put("xsList", xsList);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbappreciation:save")
    public R save(@RequestBody Map<String, Object> map){
        // 先添加欣赏
        CbAppreciationEntity appreciationEntity = new CbAppreciationEntity();
        appreciationEntity.setTid(Long.valueOf((String) map.get("tid")));
        appreciationEntity.setTypefaceName((String) map.get("typefaceName"));
        cbAppreciationService.save(appreciationEntity);
        // 再添加图片
        List<String> srcList = (List<String>) map.get("srcList");
        if(CollectionUtils.isEmpty(srcList)){
            return R.ok();
        }
        // 添加章节图片
        List<CbAppreciationPictureEntity> appreciationPictureEntities = new ArrayList<>();
        for (int i = 0; i < srcList.size(); i++) {
            CbAppreciationPictureEntity pictureEntity = new CbAppreciationPictureEntity();
            pictureEntity.setId(IdWorker.getId());
            pictureEntity.setPid(appreciationEntity.getId());
            pictureEntity.setAppreciationPicture(srcList.get(i));
            pictureEntity.setPorder(i + 1);
            appreciationPictureEntities.add(pictureEntity);
        }
        cbAppreciationPictureService.insertBatch(appreciationPictureEntities);
        return R.ok();
    }

    /**
     * 保存
     */
    @PostMapping("/save1")
    //@RequiresPermissions("sf:cbappreciation:save")
    public R save1(@RequestBody CbAppreciationEntity cbAppreciationEntity){
        cbAppreciationService.save(cbAppreciationEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbappreciation:update")
    public R update(@RequestBody CbAppreciationEntity cbAppreciation){
		cbAppreciationService.updateById(cbAppreciation);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbappreciation:delete")
    public R delete(@RequestBody Long[] ids){
		cbAppreciationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
